package com.lyz.dataStructure.algorithm.用友2020;

import java.lang.management.MemoryManagerMXBean;
import java.util.Scanner;

/**
 * @Author:759057893@qq.com Lyz
 * @Date: ${Date} 15:02
 * @Description:
 **/



public class Main {
    public static int index =0;
    private class Node{
        Node leftChild;
        Node rightChild;
        int data;
        public Node(int data){
            leftChild = null;
            rightChild = null;
            this.data =data;
        }
    }
    public void afterOrder(Node node){
        if(node!=null){
            afterOrder(node.leftChild);
            afterOrder(node.rightChild);
            System.out.println(node.data+" ");
        }
    }

    public Node process(int[] pre,int[] mid,int midStart,int midEnd){
        int mstart = midStart;
        int mend = midEnd;
        int flag = 0;
        if(index>=pre.length){
            return null;
        }
        Node node = new Node(pre[index]);
        for(int i =mstart;i<=mend;i++){
            if(mid[i]==pre[index]){
                flag = i;
            }
        }
        index++;
        if(midStart<flag)
            node.leftChild=process(pre,mid,midStart,flag-1);
        if(flag<midEnd)
            node.rightChild=process(pre,mid,flag+1,midEnd);
        return node;
    }
    //ABCDEFGH,BDCEAFHG
    //DECBHGFA
    public static void main(String[] args) {
        int[] pre ={1,2,3,4,5,6,7,8};
        int[] mid ={2,4,3,5,1,6,7,8};
        Scanner sc = new Scanner(System.in);
        Main main = new Main();
        Node root = main.process(pre,mid,0,mid.length-1);
        main.afterOrder(root);
    }
}
